﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HandScreen.DataMining;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows.Threading;
using System.Windows.Media.Animation;
using System.Collections;

namespace HandScreen.Views
{
    /// <summary>
    /// Interaction logic for WeatherView.xaml
    /// </summary>
    public partial class WeatherView : UserControl, IScrollable, ISuspendable
    {
        WeatherData[] theWeather;
        TextBlock[] theTextBlocks;
        const int nrOfReports = 6;
        const int updateInterval = 108000;
        private bool canScrollUp;
        private bool canScrollDown;
        private bool alive;

        private Thread workerThread;

        public WeatherView()
        {
            alive = true;
            InitializeComponent();
            theWeather = new WeatherData[nrOfReports];
            theTextBlocks = new TextBlock[nrOfReports*2];
            
            workerThread = new Thread(dataMineChalmersNews);
            workerThread.Start();
        }

        private void dataMineChalmersNews()
        {
            Boolean first = true;
            while (alive)
            {
                if (first)
                {
                    first = false;
                    InitializeDataMiner();
                    this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(
                        delegate()
                        {
                            AddTextBlocks();
                        }
                    ));
                }
                else
                {
                    theWeather = new WeatherData[nrOfReports];
                    InitializeDataMiner();

                    this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(
                        delegate()
                        {
                            UpdateGUI();
                        }
                    ));
                }
                try { Thread.Sleep(updateInterval); }
                catch (Exception e) { }
            }
        }
        public void UpdateGUI()
        {
            for (int i = 0; i < nrOfReports; i++)
            {
                theTextBlocks[i].Text = theWeather[i].getTime();
                theTextBlocks[nrOfReports + i].Text = theWeather[i].getDescription();
            }

        }

        private void AddTextBlocks()
        {
            for (int i = 0; i < nrOfReports; i++)
            {
                theTextBlocks[i] = new TextBlock();
                theTextBlocks[i].Text = theWeather[i].getTime();
                theTextBlocks[nrOfReports + i] = new TextBlock();
                theTextBlocks[nrOfReports + i].Text = theWeather[i].getDescription();

                theTextBlocks[i].FontSize = theTextBlocks[i].FontSize + 10;
                theTextBlocks[i].FontWeight = FontWeights.Heavy;
                theTextBlocks[nrOfReports + i].FontSize = theTextBlocks[i].FontSize;
                //theTextBlocks[i].MaxWidth = grid1.Width;
                theTextBlocks[i].TextWrapping = TextWrapping.Wrap;
                theTextBlocks[i].Margin = new Thickness(20, 5, 20, 0);
                //theTextBlocks[nrOfReports + i].MaxWidth = grid1.Width;
                theTextBlocks[nrOfReports + i].TextWrapping = TextWrapping.Wrap;
                theTextBlocks[nrOfReports + i].Margin = new Thickness(20,5,20,15);

                contentStack.Children.Add(theTextBlocks[i]);
                contentStack.Children.Add(theTextBlocks[nrOfReports + i]);
            }

        }

        private void InitializeDataMiner()
        {
            RSSManager reader = new RSSManager();
            Collection<Rss.Items> list;

            String feed = "http://www.yr.no/place/Sweden/V%C3%A4stra_G%C3%B6taland/Gothenburg/forecast.rss";

            reader.Url = feed;
            reader.GetRSSFeed();
            list = reader.RSSItems;

            try
            {
                int nrOfEntries = 0;
                if (list.Count >= nrOfReports)
                    nrOfEntries = nrOfReports;
                for (int i = 0; i < nrOfEntries; i++)
                {
                    String time = list[i].Title;
                    String description = list[i].Description.ToString();
                    theWeather[i] = new WeatherData(time, description);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        public void scrollUp()
        {
            //do nothing
        }

        public void scrollDown()
        {
            //do nothing
        }

        public bool CanScrollUp()
        {
            return false;
        }

        public bool CanScrollDown()
        {
            return false;
        }

        public void suspend()
        {
            alive = false;
            workerThread.Interrupt();
        }
    }

    public class WeatherData
    {
        String Time;
        String Description;

        public WeatherData(String Time, String Description)
        {
            this.Time = Time;
            this.Description = Description;
        }
        public String getTime()
        {
            return Time;
        }
        public String getDescription()
        {
            return Description;
        }
    }
}
